package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.UpLoadCarData;
import com.jzg.jcptadmin.viewinterface.UpLoadCarDataInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/8.
 */
public class UpLoadCarDataPresenter extends BasePresenter<UpLoadCarDataInterface> {

    private DataManager dataManager;
    private UpLoadCarDataInterface mView;
    private Subscription mSubscription;

    public UpLoadCarDataPresenter(DataManager dataManager){

        this.dataManager = dataManager;
    }
    @Override
    public void attachView(UpLoadCarDataInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void UpLodaCarData(Map<String,String> map){
        dataManager.UploadCarData(map).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<UpLoadCarData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){

                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(UpLoadCarData upLoadCarData) {
                mView.ShowUpLoadCarData(upLoadCarData);
            }
        });

    }


}
